package com.miniapp.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

public class PageDTO<T> {
    private Long pageIndex = 1L;
    private Long pageSize = 30L;
    private Long totalSize;
    private List<T> content;

    public Long getPageSize() {
        return pageSize;
    }

    // 不分页用的构造函数
    public PageDTO(List<T> content){
        this.content = content;
    }


    public PageDTO(Long pageIndex, Long pageSize, Long totalSize, List<T> content){
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalSize = totalSize;
        this.content = content;
    }

    public PageDTO(Long pageIndex, Long pageSize, long totalSize, List<T> content){
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalSize = totalSize;
        this.content = content;
    }
    public PageDTO(IPage page){
        this.pageIndex = page.getCurrent();
        this.pageSize = page.getSize();
        this.totalSize = page.getTotal();
        this.content = page.getRecords();
    }


    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalPage() {
        if(pageSize!=0){
            long result = totalSize/pageSize;
            if(totalSize % pageSize !=0){
                result ++;
            }
            return result;
        }
        return 0;
    }


    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public boolean hasPreviousPage(){
        if(isFirstPage()){
            return false;
        }
        return true;
    }
    public boolean isFirstPage(){
         if(pageIndex<=1){
             return true;
         }
        return false;
    }

    public boolean hasNextPage(){
        if(isLastPage()){
            return false;
        }else{
            return true;
        }
    }

    public boolean isLastPage(){
        if(pageIndex>=this.getTotalPage()){
            return true;
        }else{
            return false;
        }
    }

    public Long getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Long pageIndex) {
        this.pageIndex = pageIndex;
    }
}
